const {ccclass, property} = cc._decorator;

@ccclass
export default class App extends cc.EventTarget {
    private static _ins: App;

    public static get Ins(): App {
        if (!App._ins)
            App._ins = new App;
        return App._ins;
    }

    public ui: cc.Node = null;

    constructor() {
        super();

        this.on('ui-ready', this.onUIReady, this);
    }

    onUIReady() {
        this.openUI();
    }

    loadPrefab() {
        cc.resources.load('UI', cc.Prefab, (err, prefab) => {
            if (err)
                return console.error(err);

            console.log('prefab加载成功');
            App.Ins.ui = cc.instantiate(prefab);
            this.emit('ui-ready');
        })
    }

    openUI() {
        if (!this.ui)
            return this.loadPrefab();

        if (!this.ui.isValid) 
            return;

        console.log('cc.dynamicAtlasManager.enabled -> ' + cc.dynamicAtlasManager.enabled);

        let root = cc.director.getScene().getChildByName('Canvas');
        this.ui.active = true;
        root.addChild(this.ui);
    }

    closeUI() {
        this.ui.active = false;
        this.ui.parent = null;
    }
}
